package com.lianchuang.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.lianchuang.entity.BaiduFile;
import com.lianchuang.entity.BaiduFileEntity;
import com.lianchuang.entity.BaiduShareEnity;
import com.lianchuang.httpclient.HttpClientUtil;
/**
 * 百度网盘工具类
 * @author money
 *
 */
public class BaiduYunUtil {
	public static void main(String[] args) throws Exception {
		System.out.println(getShareUrl("floderName","ed2k://|file|%E9%9D%92%E6%98%A5%E9%80%97.720p.HD%E5%9B%BD%E8%AF%AD%E4%B8%AD%E5%AD%97[www.66e.cc].mkv|1249659797|09C1C6D2FEAADF5E4FED8B8799178398|h=54PQTM67A47IN5LVEGPZRHJGGHU7BUAL|/"));
	}
	
	public static String getShareUrl(String name,String downUrl){
		String filePath = "/movie/"+name;
		boolean result = createFloder(filePath);
		sleep(1000);
		if(result){
			result = downLoad(downUrl, filePath+"/");
			if(result){
				sleep(1000);
				Map<String,String> res = getFileShareUrl(filePath);
				if(res != null){
					return res.get("url");
				}				
			}
		}
		return null;
	}
	
	/**
	 * 避免速度快封号
	 * @param miseconds
	 */
	public static void sleep(long miseconds){
		try {
			Thread.sleep(miseconds);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 设置头部参数
	 * @return
	 */
	public static Map<String,String> getHeader(){
		 Map<String,String> header  = new HashMap<String,String>();
		// header.put("Cookie", "BAIDUID=6010734B0C41B597303593D4D0BB3821:FG=1; FP_UID=7523037a1ac7aad9bc036f4db2e23e18; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; BIDUPSID=BFDC7C6E972152DB5A75483E1AFC7137; PSTM=1516169061; H_PS_PSSID=1454_21103; PSINO=2; BDRCVFR[eHt_ClL0b_s]=mk3SLVN4HKm; panlogin_animate_showed=1; BDUSS=VhvbnNOTjlHSUlWc3ppYzZWTEdHTTlMb0NFNlNIakdWVzhSMk5EQmljcXZnNFphQVFBQUFBJCQAAAAAAAAAAAEAAACI01ALemhvbmdxaWFuMTYzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAK~2Xlqv9l5aL; pan_login_way=1; PANWEB=1; STOKEN=ca0f6d0dfc1943c265a23acc651b49dfbcfc9749181fa038e50ad6810ec9f352; SCRC=64ed282ac21bc576c34ae3baf7b631ce; PANPSC=16920131119557134939%3AkpNhOLMYOxTT4pQJRadfsqikII2LTYQXXVlXlVK6ehym%2F7u4fX79ZROAD5r1J1nbJHUMfeG8h321%2Flnio%2FCnqw6AWDn146o0dc1YK18SpsKP8XxTJKWpuO1miXCp7vh%2BLfCxbG1iv14rWOqA3q3PCcDuiFJchHVyObuDsIux8jhE%2BhLVkDMtikks6x2KH49U; Hm_lvt_7a3960b6f067eb0085b7f96ff5e660b0=1516172917; Hm_lpvt_7a3960b6f067eb0085b7f96ff5e660b0=1516173080; cflag=15%3A3");
		header.put("Cookie", "BAIDUID=6010734B0C41B597303593D4D0BB3821:FG=1; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; BIDUPSID=BFDC7C6E972152DB5A75483E1AFC7137; PSTM=1516169061; H_PS_PSSID=1454_21103; panlogin_animate_showed=1; PANWEB=1; STOKEN=b9995c263c4e6f2f20fc59c2724636137577c2aecb0082dd81f21d1afa6f5ca4; SCRC=a4f449ab1bdc5ef0eb59645d9b31e7fc; PANPSC=15874923431864426693%3AkpNhOLMYOxTT4pQJRadfsqikII2LTYQXXVlXlVK6ehym%2F7u4fX79ZROAD5r1J1nb6r5Obga4OC3aamkVOF8CPQ6AWDn146o0dc1YK18SpsKP8XxTJKWpuO1miXCp7vh%2BLfCxbG1iv14rWOqA3q3PCcDuiFJchHVyObuDsIux8jhE%2BhLVkDMtikks6x2KH49U; Hm_lvt_7a3960b6f067eb0085b7f96ff5e660b0=1516172917; Hm_lpvt_7a3960b6f067eb0085b7f96ff5e660b0=1516327693; BDUSS=5QVGZKVWstOEM0ZDRvbWRqcVRBUklBVVZZdzV5Y05JNlk3UmtIYnE3UUIxSWRhQVFBQUFBJCQAAAAAAAAAAAEAAACI01ALemhvbmdxaWFuMTYzAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFHYFoBR2BaY3; SIGNIN_UC=70a2711cf1d3d9b1a82d2f87d633bd8a02662763499; BDSFRCVID=NWtsJeC62GBBdo6ADJxPkVrpEfewQN3TH6aIehZaBvcK7hmM2SnMEG0Pqx8g0KubaWp1ogKK0eOTHk6P; H_BDCLCKID_SF=tJPj_KLMtKt3fRTxe5AWqR0qDa0jJD62aJ3WBljbWJ5TMCo6Dl5J-RDFQ-cj2tnp-jR4LPou-nO_ShPC-tnHqq3-3fOR5-LJtRIqsRnz3l02VhbEe-t2ynLVyp7ft4RMW20j0l7mWnk-VKF4DTLbDTJLjNReaI6Q2C6XBJTqHJOoDDv83nO5y4LdjG5x3fjbabcCKpcyLnv4q4JHb4ob0f6y3-Aq5xc-2NQK5tQJWK3sDqC40PTYQfbQ0hQOqP-jW5Ta3x3MtR7JOpkxbfnxy-nB0a62btt_JnFt_IQP; PSINO=2; cflag=15%3A3; BDRCVFR[eHt_ClL0b_s]=mk3SLVN4HKm; BCLID=9345408311771696022");
		 header.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		 return header;
	}
	/**
	 * 获取分享链接
	 *分享filePath下 第一个文件
	 * @return
	 */
	public static Map<String,String> getFileShareUrl(String filePath){
		//获取文件列表
		try {
			Map<String,String> map = new HashMap<String,String>();
			String url = "https://pan.baidu.com/api/list?dir="+filePath
					+ "&bdstoken=daa10770831206688bcd6416807d81d4&logid=MTUxNjMyOTk0NTg2MjAuOTk1Njk4NzY1NTAzODY5Mw==&num=100&"
					+ "order=time&desc=1&clienttype=0&showempty=0&web=1&page=1&channel=chunlei&web=1&app_id=250528";
			String result = HttpClientUtil.get(url,"UTF-8",getHeader());
			BaiduFile baiduFile = new Gson().fromJson(result, BaiduFile.class);
			if(baiduFile != null && baiduFile.getList() != null && baiduFile.getList().size() > 0){
				BaiduFileEntity bfe = baiduFile.getList().get(0);//每个文件夹下一个文件
				String fs_id = bfe.getFs_id();
				List<String> fList = new ArrayList<String>();
				fList.add(fs_id);
				String shareUrl = "https://pan.baidu.com/share/set?channel=chunlei&clienttype=0&web=1&channel=chunlei&web=1&app_id=250528&bdstoken=daa10770831206688bcd6416807d81d4&logid=MTUxNjMzMzc5MzU5MzAuNjg2ODU4OTYyMzUzMTcwMw==&clienttype=0";
				Map<String,Object> params = new HashMap<String,Object>();
				params.put("fid_list", fList);
				params.put("schannel", "4");
				params.put("period", "0");
				params.put("pwd", "kjrb");
				params.put("channel_list", new ArrayList<String>());
				String shareBack = HttpClientUtil.post(shareUrl, params, "UTF-8", getHeader());
				System.out.println("分享返回----》"+shareBack);
				BaiduShareEnity bse = new Gson().fromJson(shareBack, BaiduShareEnity.class);
				if(bse!= null){
					map.put("url", bse.getLink());
					return map;
				}
				return null;
			}
		} catch (JsonSyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 创建文件夹
	 * @param path
	 * @return
	 */
	public static boolean createFloder(String path){
		String url = "https://pan.baidu.com/api/create?a=commit&channel=chunlei&web=1&app_id=250528&bdstoken=daa10770831206688bcd6416807d81d4&logid=MTUxNjMyODAwMTk4NjAuOTg4MjgzODg5Njk1NDAyMg==&clienttype=0";
		 Map<String,Object> params = new HashMap<String, Object>();
		 params.put("path", path);
		params.put("isdir", 1);
		try {
			String result = HttpClientUtil.post(url, params,"UTF-8",getHeader());
			System.out.println("创建文件夹返回--》"+result);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
		
	}
	/**
	 * 离线下载
	 * @param sourceUrl
	 * @return
	 */
	  private static boolean downLoad(String sourceUrl,String savePath){
		  try {
			String str = "https://pan.baidu.com/rest/2.0/services/cloud_dl?"
			    		+ "channel=chunlei"
			    		+ "&web=1"
			    		+ "&app_id=250528"
			    		+ "&bdstoken=6180f8721e9665cbae27f9d1991f72d1"
			    		+ "&logid=MTUxNjE2MjI2MTIxOTAuMDM3OTE1NTMwNTk2NjUzOTg="
			    		+ "&clienttype=0";
			 Map<String,Object> params = new HashMap<String, Object>();
			 params.put("method", "query_magnetinfo");
			 params.put("app_id", "250528");
			 params.put("source_url", sourceUrl);
			 params.put("save_path", savePath);
			 params.put("type", "4");
			 
			 Map<String,String> header  = getHeader();
			
			System.out.println(HttpClientUtil.post(str,params,"UTF-8",header));
			
			params.put("method", "add_task");

			String url = "https://pan.baidu.com/rest/2.0/services/cloud_dl?channel=chunlei&web=1&app_id=250528&bdstoken=89a20e813efa5eb5292755a3f080a58e&logid=MTUxNjE3MzM2ODg1MzAuNzIyNTAzMTkwNzQxOTM5OA==&clienttype=0";
			System.out.println(HttpClientUtil.post(url,params,"UTF-8",header));
		} catch (Exception e) {
			
			e.printStackTrace();
			return false;
		}
		  return true;
	   }
	 
}
